<?php

/**
 *  登陆页
 * @file    
 * @date    
 * @author  
 * @version    
 */

namespace app\backend\controller;

use think\Request;
use think\Db;
use think\Session;
//use think\console\Command;
use think\Controller;
//use think\console\command\make\Controller;

class SeniorAdministrator extends Common {

    /**
     * 主页面
     */
    public function index() {
        $result=db("administrators")->select();
        $this->assign('lists',$result);
        return $this->fetch();
    }
    
    public function change_password() {
        $data = input();
        //var_dump($data);
        if(!$data){
            return $this->fetch();
        }
        //判断原始密码是否正确
        $res=db('administrators')->field('password')->where('uid',session('user_id'))->find();
        $tem=$data['password0'].Session::get('user_name');
        if(MD5($tem)!=$res['password']){
            $this->error('修改失败，原始密码不正确!');
        }
        //判断两次密码是否一致
        if (!$data['password1']) {
           unset($data['password1']);
        } else {
            if ($data['password1'] != $data['password2']) {
                $this->error('两次密码不一致!');
            }
            $salt=session('user_name');
            $data['password1'] = md5($data['password1'].$salt);
        }
        $res = db::table('administrators')->where('uid',session('user_id'))->update(['password'=>$data['password1']]);
        if ($res) {
            $data=['time'=>date('Y-m-d H:i:s',time()),'ip'=>request()->ip(),'event'=>'修改密码','result'=>'成功','user'=>session('user_id')];
            db('logs')->insert($data);
           $this->success('密码修改成功，请重新登录',"Login/index");
        } else {
            $data=['time'=>date('Y-m-d H:i:s',time()),'ip'=>request()->ip(),'event'=>'修改密码','result'=>'失败','user'=>session('user_id')];
            db('logs')->insert($data);
           $this->error('修改失败');
        }
    }
    
    public function course_list(){
        $result=db("courses")->select();
        $this->assign('lists',$result);
        return $this->fetch();
    }
    
    public function course_add(){
        $data = input();
        //判断数据不为空
        if(!$data){
            return $this->fetch();
        }
        $res=db('courses')->where('course_id',$data['course_id'])->find();
        if($res){
            $this->error('该课程号已经存在，请勿重复添加！');
        }
        $res=db('courses')->where('course_time',$data['course_time'])
            ->where('course_place',$data['course_place'])->find();
        if($res){
            $this->error('该时间段的教室已经存在课程，添加失败！');
        }
        $res=db('courses')->insert($data);
        if ($res) {
            $data=['time'=>date('Y-m-d H:i:s',time()),'ip'=>request()->ip(),'event'=>'添加课程','result'=>'成功','user'=>session('user_id')];
            db('logs')->insert($data);
            $this->success('添加成功','senior_administrator/course_list');
        } else {
            $data=['time'=>date('Y-m-d H:i:s',time()),'ip'=>request()->ip(),'event'=>'添加课程','result'=>'失败','user'=>session('user_id')];
            db('logs')->insert($data);
            $this->error('添加失败');
        }
    }
    
    public function course_edit(){
        $data = input();
        if(!$data){
            return $this->fetch();
        }
        $res=db('courses')->where('course_id',$data['course_id'])->find();
        if(!$res){
            $this->error('该课程不存在，无法修改！');
        }
        if($res['course_time']!=$data['course_time']||$res['course_place']!=$data['course_place']){
            $res=db('courses')->where('course_time',$data['course_time'])
                ->where('course_place',$data['course_place'])->find();
            if($res){
                $this->error('该时间段的教室已经存在课程，修改失败！');
            }
        }
        $res = db::table('courses')->where('course_id',$data['course_id'])
            ->update([
                'course_name' => $data['course_name'],
                'course_time' => $data['course_time'],
                'course_place' => $data['course_place'],
                'course_nums' => $data['course_nums'],
            ]);
        if ($res) {
            $data=['time'=>date('Y-m-d H:i:s',time()),'ip'=>request()->ip(),'event'=>'修改课程','result'=>'成功','user'=>session('user_id')];
            db('logs')->insert($data);
            $this->success('修改成功','senior_administrator/course_list');
        } else {
            $data=['time'=>date('Y-m-d H:i:s',time()),'ip'=>request()->ip(),'event'=>'修改课程','result'=>'失败','user'=>session('user_id')];
            db('logs')->insert($data);
            $this->error('修改失败');
        }
    }
    
    public function course_delete(){
        $data = input();
        if(!$data){
            return $this->fetch();
        }
        $res=db('courses')->where('course_id',$data['course_id'])->find();
        if(!$res){
            $this->error('该课程不存在，无法删除！');
        }
        $res=db('courses')->where('course_id',$data['course_id'])->delete();
        if ($res) {
            $data=['time'=>date('Y-m-d H:i:s',time()),'ip'=>request()->ip(),'event'=>'删除课程','result'=>'成功','user'=>session('user_id')];
            db('logs')->insert($data);
            $this->success('删除成功','senior_administrator/course_list');
        } else {
            $data=['time'=>date('Y-m-d H:i:s',time()),'ip'=>request()->ip(),'event'=>'删除课程','result'=>'失败','user'=>session('user_id')];
            db('logs')->insert($data);
            $this->error('删除失败');
        }
    }

    public function administrators_list(){
        $result=db('administrators')->select();
        $this->assign('lists',$result);
        return $this->fetch();
    }

    public function administrators_add(){
        $data = input();
        if(!$data){
            return $this->fetch();
        }
        $res=db('administrators')->where('username',$data['username'])->find();
        if($res){
            $this->error('该用户已经存在，请勿重复添加！');
        }
        if($data['password']!=$data['rpassword']){
            $this->error('两次输入密码不一致，请重新输入');
        }
        $data['password']=MD5($data['password'].$data['username']);
        $userinfo=['username'=>$data['username'],'password'=>$data['password'],'role'=>$data['role']];
        $res=db('administrators')->insert($userinfo);
        if (!$res) {
            $data=['time'=>date('Y-m-d H:i:s',time()),'ip'=>request()->ip(),'event'=>'添加管理员','result'=>'成功','user'=>session('user_id')];
            db('logs')->insert($data);
            $this->error('添加失败');
        }else{
            $data=['time'=>date('Y-m-d H:i:s',time()),'ip'=>request()->ip(),'event'=>'添加管理员','result'=>'失败','user'=>session('user_id')];
            db('logs')->insert($data);
            $this->success('添加成功');
        }
    }
    
    public function administrators_delete(){
        $data = input();
        if(!$data){
            return $this->fetch();
        }
        $res=db('administrators')->where('username',$data['username'])->find();
        if(!$res){
            $this->error('该用户不存在，无法删除！');
        }
        $res=db('administrators')->where('username',$data['username'])->delete();
        if ($res) {
            $data=['time'=>date('Y-m-d H:i:s',time()),'ip'=>request()->ip(),'event'=>'删除管理员','result'=>'成功','user'=>session('user_id')];
            db('logs')->insert($data);
            $this->success('删除成功');
        } else {
            $data=['time'=>date('Y-m-d H:i:s',time()),'ip'=>request()->ip(),'event'=>'删除管理员','result'=>'失败','user'=>session('user_id')];
            db('logs')->insert($data);
            $this->error('删除失败');
        }
    }
    
    public function students_list(){
        $result=db("students")->select();
        $this->assign('lists',$result);
        return $this->fetch();
    }
    
    public function students_add(){
        $data = input();
        if(!$data){
            return $this->fetch();
        }
        $res=db('students')->where('stu_id',$data['stu_id'])->find();
        if($res){
            $this->error('该学生已经存在，请勿重复添加！');
        }
        if($data['password']!=$data['rpassword']){
            $this->error('两次输入密码不一致，请重新输入');
        }
        $data['password']=MD5($data['password'].$data['stu_id']);
        $userinfo=['stu_id'=>$data['stu_id'],'stu_name'=>$data['stu_name'],'stu_pwd'=>$data['password']];
        $res=db('students')->insert($userinfo);
        if ($res) {
            $data=['time'=>date('Y-m-d H:i:s',time()),'ip'=>request()->ip(),'event'=>'添加学生','result'=>'成功','user'=>session('user_id')];
            db('logs')->insert($data);
            $this->success('添加成功');
        } else {
            $data=['time'=>date('Y-m-d H:i:s',time()),'ip'=>request()->ip(),'event'=>'添加学生','result'=>'失败','user'=>session('user_id')];
            db('logs')->insert($data);
            $this->error('添加失败');
        }
    }

    public function students_delete(){
        $data = input();
        if(!$data){
            return $this->fetch();
        }
        $res=db('students')->where('stu_id',$data['stu_id'])->find();
        if(!$res){
            $this->error('该用户不存在，无法删除！');
        }
        $res=db('students')->where('stu_id',$data['stu_id'])->delete();
        if ($res) {
            $data=['time'=>date('Y-m-d H:i:s',time()),'ip'=>request()->ip(),'event'=>'删除学生','result'=>'成功','user'=>session('user_id')];
            db('logs')->insert($data);
            $this->success('删除成功');
        } else {
            $data=['time'=>date('Y-m-d H:i:s',time()),'ip'=>request()->ip(),'event'=>'删除学生','result'=>'失败','user'=>session('user_id')];
            db('logs')->insert($data);
            $this->error('删除失败');
        }
    }

    public function logs_list(){
        $result=db("logs")->select();
        $this->assign('lists',$result);
        return $this->fetch();
    }
    /*
    public function time_set() {
        $data = input();
        if(!$data){
            return $this->fetch();
        }
        session('time', $data['time']);
        $redis=new \Redis();
        $redis->connect(Config("127.0.0.1"),Config("6379"));
        $redis->set('time',$data['time']);
        echo $redis->get('time');
    }
    */
    public function logout() {
        $data=['time'=>date('Y-m-d H:i:s',time()),'ip'=>request()->ip(),'event'=>'登出','result'=>'成功','user'=>session('user_name')];
        db('logs')->insert($data);
        session('user_name', null);
        session('user_id', null);
        $this->success('退出成功', 'login/index');
    }
    
}
